package lab_11.com.acme.bankapp.service.bank;

import java.util.ArrayList;

import lab_11.com.acme.bankapp.domain.bank.Bank;
import lab_11.com.acme.bankapp.domain.bank.CheckingAccount;
import lab_11.com.acme.bankapp.domain.bank.Client;
import lab_11.com.acme.bankapp.domain.bank.ClientRegistrationListener;
import lab_11.com.acme.bankapp.domain.bank.Gender;
import lab_11.com.acme.bankapp.domain.bank.SavingsAccount;
import lab_11.com.acme.bankapp.domain.bank.Exceptions.ClientExistsException;
import lab_11.com.acme.bankapp.domain.bank.Exceptions.ClientNumberExceeded;
import lab_11.com.acme.bankapp.domain.bank.Exceptions.NotEnoughFundsException;
import lab_11.com.acme.bankapp.domain.bank.Exceptions.OverdraftLimitExceededException;

public class BankApplication {

	public static void main(String[] args) {

		ArrayList<ClientRegistrationListener> listeners = new ArrayList<ClientRegistrationListener>();
		listeners.add(new Bank.EmailNotificationListener());
		listeners.add(new Bank.PrintClientListener());

		Bank bank = new Bank(listeners);

		System.out.println("Creating first client with savings account.");
		Client client1;
		try {
			client1 = BankService.addClient(bank, new Client(Gender.FEMALE,
					"Lena", new SavingsAccount(10)));
		} catch (ClientExistsException e1) {
			System.out.println("A client with the same name already exists.");
			return;
		} catch (ClientNumberExceeded e1) {
			System.out
					.println("Maximum number of clients has already been reached. Can't add a new one.");
			return;
		}
		// Info will be printed through invocation of PrintClientListener

		System.out.println("Adding second checking account.");
		try {
			client1.addClientAccount(new CheckingAccount(15, 10));
		} catch (IllegalArgumentException e1) {
			System.out
					.println("Balance negative value shouldn't exceed the overdraft.");
		}
		client1.printAllClientData();

		System.out.println("Putting 6 on the first account.");
		try {
			client1.getAccounts().get(0).deposit(6);
		} catch (IllegalArgumentException e1) {
			System.out.println("Negative amount value!");
		}
		client1.printAllClientData();

		// Testing NotEnoughFundsException (not exception should be raised,
		// as there may be negative values of balance for a Saving account)
		System.out.println("Withdrawing 20 from the first account.");
		try {
			client1.getAccounts().get(0).withdraw(20);
		} catch (NotEnoughFundsException e) {
			System.out.println("Not enough funds!");
		} catch (IllegalArgumentException e) {
			System.out.println("Negative amount value!");
		}
		System.out.println("Done!");
		client1.printAllClientData();

		// Testing NotEnoughFundsException for CheckingAccount
		System.out.println("Withdrawing 27 from the second account.");
		try {
			client1.getAccounts().get(1).withdraw(27);
		} catch (OverdraftLimitExceededException e) {
			System.out.println("Not enough funds!");
			System.out.format("You can't withdraw more than %1$f!\n",
					e.maxAmountToWithraw);
		} catch (IllegalArgumentException e) {
			System.out.println("Negative amount value!");
		}
		client1.printAllClientData();

		// Testing NotEnoughFundsException for SavingsAccount
		System.out.println("Putting -5 on the first account.");
		try {
			client1.getAccounts().get(0).deposit(-5);
		} catch (IllegalArgumentException e) {
			System.out.println("Negative amount value!");
		}
		client1.printAllClientData();

		// Testing NotEnoughFundsException for CheckingAccount
		System.out.println("Putting -5 on the second account.");
		try {
			client1.getAccounts().get(1).deposit(-5);
		} catch (IllegalArgumentException e) {
			System.out.println("Negative amount value!");
		}
		client1.printAllClientData();

		// Creating CheckingAccount with less money than overdraft limit
		try {
			client1.addClientAccount(new CheckingAccount(-10, 5));
		} catch (IllegalArgumentException e) {
			System.out
					.println("Balance negative value shouldn't exceed the overdraft.");
		}

		// Testing ClientExistsException
		try {
			System.out
					.println("Creating second client with the same name as the fist client");
			@SuppressWarnings("unused")
			Client client2 = BankService.addClient(bank, new Client(
					Gender.FEMALE, "Lena", new CheckingAccount(5, 5)));
		} catch (ClientExistsException e) {
			System.out.println("A client with the same name already exists!");
		} catch (ClientNumberExceeded e) {
			System.out
					.println("Maximum number of clients has already been reached. Can't add a new one.");
		} catch (IllegalArgumentException e) {
			System.out
					.println("Balance negative value shouldn't exceed the overdraft.");
		}
	}

}
